package com.androidy;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.TextUtils.TruncateAt;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class common {

	public static ProgressDialog generate_loading_message(Context context){
		return ProgressDialog.show(context, "Downloading...", "Please wait until download is completed.");
	}
	
	public static void dl_image(ImageView msource, String murl){
		new download_image(msource, murl).execute();		
	}
	
	public static class download_image extends AsyncTask<Void, Void, Void> {
		ProgressDialog progressDialog = null;
		ImageView source = null;
		Bitmap image = null;
		String url = null;
		
		public download_image(ImageView msource, String murl){
			source = msource;
			url = murl;			
		}
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			image = dbConnection.getBitmapFromURL(url);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			source.setImageBitmap(image);
		}
	}


	public static LinearLayout icon_header_cell(Context context, ImageView image, String head) {
		LinearLayout child = new LinearLayout(context);
    	child.setOrientation(0); //horizontal
    	LinearLayout.LayoutParams cell1 = new LinearLayout.LayoutParams(100, 120);
    	LinearLayout.LayoutParams cell2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 120);
    	cell1.setMargins(0, 0, 0, 0);
    	cell2.setMargins(0, 0, 0, 0);
    	
    	//set thumb pic
    	image.setMaxHeight(70);
    	image.setPadding(10, 0, 0, 0);
    	
    	//set heading
    	TextView cell_header = new TextView(context);
    	cell_header.setText(head);
    	cell_header.setWidth(480);
    	cell_header.setPadding(20, 35, 20, 0);
    	cell_header.setTextSize(20);
    	cell_header.setTextColor(Color.rgb(24, 61, 150));
    	cell_header.setSingleLine(true);
    	cell_header.setEllipsize(TruncateAt.END);

    	//construct a cell
    	child.addView(image, cell1);
    	child.addView(cell_header, cell2);
    	child.setBackgroundResource(R.drawable.cell_background);
    	
    	return child;
	}
}
